package wangshuai.risk.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.faces.application.Application;
import javax.mail.internet.NewsAddress;

import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Component;

import com.sun.org.apache.bcel.internal.generic.NEW;

import fanjingzhu.check.dao.ApplicationDao;

import publics.model.Ratio;
import wangshuai.risk.algorithm.RatioAnalyse;
import wangshuai.risk.dao.EnterpriseRiskDao;
import wangshuai.risk.dao.RatioDao;
import wangshuai.risk.dao.impl.EnterpriseRiskDaoImpl;
import wangshuai.risk.service.EnterpriseRiskService;

/**
 * @author wangshuai 
 * @expiain  本类为企业风险分析类，提供企业 整体（综合）风险分析
 */
@Component("enterpriseRiskService")
public class EnterpriseRiskServiceImpl implements EnterpriseRiskService {

	/*EnterpriseRiskDao enterpriseRiskDao;

	public EnterpriseRiskDao getEnterpriseRiskDao() {
		return enterpriseRiskDao;
	}
	@Resource(name="enterpriseRiskDao")
	public void setEnterpriseRiskDao(EnterpriseRiskDao enterpriseRiskDao) {
		this.enterpriseRiskDao = enterpriseRiskDao;
		System.out.println("enterpriseRiskDao injected in EnterpriseRiskServiceImpl!");
	}*/
	private RatioDao ratioDao; //注入ratioDao的bean
	private ApplicationDao applicationDao;
	
	public RatioDao getRatioDao() {
		return ratioDao;
	}

	@Resource(name="ratioDao")
	public void setRatioDao(RatioDao ratioDao) {
		this.ratioDao = ratioDao;
		System.out.println("RatioDaoImpl inject in EnterpriseRiskService!");
	}
	public ApplicationDao getApplicationDao() {
		return applicationDao;
	}
	@Resource(name="applicationDao")
	public void setApplicationDao(ApplicationDao applicationDao) {
		this.applicationDao = applicationDao;
		System.out.println("ApplicationDaoImpl inject in EnterpriseRiskService!");
	}



	private List<Ratio> ratios;
	private RatioAnalyse ratioAnalyse = new RatioAnalyse();
	private String result = null;
	private publics.model.Application application;

	/**
	 * 企业风险分析函数，
	 * 分析成功后，会把分析结果 写入 企业贷款 申请表 的 风险系数 和 风险描述 字段。
	 * @param appId   企业 申请表id号
	 * @return boolean  分析成功，返回true ， 分析失败 返回false。
	 */
	public boolean riskAnalyze(int appId) {//此方法还未完成
		
		if (appId <= 0) {
			System.out.println("企业申请表id不合法，无法进行风险分析！appId="+appId); // 测试用
			return false;
		}
		
		ratios = this.getRatioDao().findByAppId(appId);//通过企业id查找比率分析表
		if(ratios == null){
			System.out.println("未找到比率分析表！");
			return false;
		}else{	
			result = "";
			for (int i =0; i < ratios.size(); i++){			
				result += ratioAnalyse.ratioAnalyse(ratios.get(i));//单个比率表分析
				
			}	
			if(result == null){
				System.out.println("财务比率表分析失败！");
				return false;
			}
			application = this.getApplicationDao().findById(appId);
			application.setDescribe(result);
				
				//此处增加 风险系数分析代码！！！
			String s = "5.6";
			application.setRate(Float.valueOf(s));//测试用
			
			//此处 更新 application 表,保存分析结果
			if(!this.getApplicationDao().update(application)){
				System.out.println("保存ratio分析结果失败！");
				return false;
			}
			
		}
		
		
		return true;
	}
	

}
